package com.wenwu.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsLiterature;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.dao.RelicsAttachmentMapper;
import com.wenwu.dao.RelicsLiteratureMapper;
import com.wenwu.service.LiteratureService;

@Service
public class LiteratureServiceImpl implements LiteratureService {

	@Autowired
	RelicsLiteratureMapper relicsLiteratureMapper;
	@Autowired
	private RelicsAttachmentMapper attachmentMapper;
	public List<RelicsLiterature> selectAll() throws RelicsException {
		List<RelicsLiterature> list = null;
		try {
			list = relicsLiteratureMapper.selectAll();
		} catch (SQLException e) {
			throw new RelicsException("对不起服务器异常请稍后重试！");
		} catch (Exception e) {
			throw new RelicsException("对不起服务器异常请稍后重试！");
		}
		return list;
	}
	public RelicsLiterature selectByKey(Integer id) throws RelicsException {
		RelicsLiterature rl = null;
		try {
			rl = relicsLiteratureMapper.selectByPrimaryKey(id);
		} catch (SQLException e) {
			throw new RelicsException("对不起服务器异常请稍后重试！");
		} catch (Exception e) {
			throw new RelicsException("对不起服务器异常请稍后重试！");
		}
		return rl;
	}
	
	public String deleteBykeys(String keys) throws RelicsException {
		String result = "删除失败！";
		if (keys != null) {
			try {
				List<String> keyList = new ArrayList<String>();
				String[] objs = keys.split(",");
				for (String k : objs) {
					keyList.add(k);
				}
				result = "成功删除" + relicsLiteratureMapper.deleteInfoByKey(keyList) + "条数据";
			} catch (Exception e) {
				throw new RelicsException("系统错误，请稍后再试", e);
			}
		}
		return result;
	}
	
	public String addObj(RelicsLiterature relicsLiterature)throws RelicsException{
		try{
			String result = null;
			int num = 0 ;
			List<RelicsAttachment> list = relicsLiterature.getFileList();
			if (relicsLiterature.getLiteratureId()== null) {
				num += relicsLiteratureMapper.insertSelective(relicsLiterature);
				if (relicsLiterature.getLiteratureId() > 0) {
					if (list != null && list.size() > 0) {
						for (RelicsAttachment attachment : list) {
							attachment.setAttachmentDesc("其他实物资料附件");
							attachment.setTableOwner(relicsLiterature.getLiteratureId());// 外键
						}
						num += attachmentMapper.insertList(list);
					}
				}
				if (num > 0) {
					result = "其他实物资料添加成功";
				} else {
					result = "其他实物资料添加失败";
				}
			} else {
				num += attachmentMapper.deleteByKey(relicsLiterature.getLiteratureId(),relicsLiterature.getOwnList());//先删除原有的附件，再添加
				if (list != null && list.size() > 0) {
					for (RelicsAttachment attachment : list) {
						attachment.setAttachmentDesc("其他实物资料附件");
						attachment.setTableOwner(relicsLiterature.getLiteratureId());// 外键
					}
					num += attachmentMapper.insertList(list);
				}
				num += relicsLiteratureMapper.updateByPrimaryKeySelective(relicsLiterature);
				if (num > 0) {
					result = "其他实物资料修改成功";
				} else {
					result = "其他实物资料修改失败";
				}
			}
			return result;
		}catch(Exception e){
			throw new RelicsException("系统错误，请稍后再试",e);
		}
	}
}
